package com.share.web.action.oa.org;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.share.bean.oa.org.Department;
import com.share.bean.oa.org.Employee;
import com.share.bean.oa.org.Organization;
import com.share.commons.base.query.PageView;
import com.share.commons.util.DateUtil;
import com.share.commons.util.EncodingUtil;
import com.share.commons.util.UUIDGenerator;
import com.share.commons.util.FileUtil;
import com.share.commons.util.WebSiteUrl;
import com.share.commons.util.ValidateFile;
import com.share.service.oa.org.DepartmentManager;
import com.share.service.oa.org.EmployeeManager;
import com.share.service.oa.org.OrganizationManager;
import com.share.web.action.base.BaseActionSupport;
import com.share.web.formbean.oa.hr.DepartmentForm;
import com.share.web.formbean.oa.hr.EmployeeForm;
import com.share.web.formbean.upload.UploadFileForm;

@Controller("webEmployeeAction")
@Scope("prototype")
public class EmployeeAction extends BaseActionSupport{

	private static final long serialVersionUID = 1L;
	
	private Employee emp ;
	
	private EmployeeForm empform ;
	
	private UploadFileForm fileform ;
	
	@Resource
	private PageView<Employee> pageView ;
	
	@Resource
	private EmployeeManager empMgr ;
	
	@Resource
	private OrganizationManager orgMgr ;

	@Resource
	private DepartmentManager deptMgr ;
	
	/**
	 * 添加员工界面
	 */
	public String regEmployeeUI() {
		try {
			if("orgUI".equals(this.getBaseform().getType())) {
				List<Organization> orgList = this.orgMgr.getOrgAll() ;
				if(null != orgList && !orgList.isEmpty()) {
					this.getBaseform().getRequest().setAttribute("orgList",orgList) ;
					return "regEmpStep1" ;
				} else {
					this.getBaseform().getRequest().setAttribute("cueMsg","【未查找到组织机构信息，请先添加】") ;
					this.getBaseform().getRequest().setAttribute("redirectUrl",WebSiteUrl.readUrl("web.organization.addOrg", this.getBaseform().getRequest())) ;
					return "cueMsg" ;
				}
			} 
			if("deptUI".equals(this.getBaseform().getType())) {
				List<Department> deptList = this.deptMgr.findDeptManager(new DepartmentForm("byOrgCode",empform.getOrgCode())) ;
				if(null != deptList && !deptList.isEmpty()) {
					this.getBaseform().getRequest().setAttribute("empform",empform) ;
					this.getBaseform().getRequest().setAttribute("deptList",deptList) ;
					return "regEmpStep2" ;
				} else {
					this.getBaseform().getRequest().setAttribute("cueMsg","【未查找到该机构部门信息，请先添加】") ;
					this.getBaseform().getRequest().setAttribute("redirectUrl",WebSiteUrl.readUrl("web.department.getdept", this.getBaseform().getRequest())) ;
					return "cueMsg" ;
				}
			} 
			if("empUI".equals(this.getBaseform().getType())) {
				
				if(null != empform.getDeptNo() && !"".equals(empform.getDeptNo())) {
					this.getBaseform().getRequest().setAttribute("empform",empform) ;
					return "regEmp" ;
				} else {
					this.getBaseform().getRequest().setAttribute("errorsMsg","【请选择部门】") ;
					return "errors" ;
				}
			} else {
				this.getBaseform().getRequest().setAttribute("errorsMsg","【访问添加员工界面出错了】") ;
				return "errors" ;
			}
			
		} catch (Exception e) {
			e.printStackTrace() ;
			this.getBaseform().getRequest().setAttribute("errorsMsg","【访问添加员工界面出异常...】") ;
			return "errors" ;
		}
	}
	
	/** 添加员工 */
	public String regEmployee() {
		//图片保存目录
		String saveDir = this.getBaseform().getRealPath()+"/upload/images/" ;
		
		try {
			//判断上传图片
			if(null != fileform && fileform.getFile().length() > 0 && ValidateFile.imageType(fileform)) {
				String ext = UUIDGenerator.getUUID() + "." +ValidateFile.getExt(fileform.getFileFileName()) ;
				emp.setEmpImage(ext) ;
				//上传图片
				if(!FileUtil.saveFile(fileform,saveDir,ext)) 
					this.getBaseform().getRequest().setAttribute("uploadMsg","上传图片失败") ;
			} else {
				this.getBaseform().getRequest().setAttribute("uploadMsg","上传图片失败，该文件内容为空") ;
				this.getBaseform().getRequest().setAttribute("redirectUrl",WebSiteUrl.readUrl("web.employee.mgrEmp",this.getBaseform().getRequest())) ;
			}
			
			//设置图片名称
			this.empMgr.saveEmpManager(emp) ;
			this.getBaseform().getRequest().setAttribute("uploadMsg","添加员工成功") ;
			this.getBaseform().getRequest().setAttribute("redirectUrl",WebSiteUrl.readUrl("web.employee.mgrEmp",this.getBaseform().getRequest())) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		return "addEmpMsg" ; 
	}

	
	/** 查询所有员工 */
	public String managerEmployee() {
		try {
			if(null != empform) {
				StringBuffer urlParam = new StringBuffer() ;
				
				if(null != empform.getStartDate() && null != empform.getEndDate()) {
					urlParam.append("&empform.startDate=" + DateUtil.format(empform.getStartDate(), DateUtil.TYPE_DATE))
					.append("&empform.endDate=" + DateUtil.format(empform.getEndDate(), DateUtil.TYPE_DATE));
				} else if(null != empform.getKeyword() && !empform.getKeyword().equals("")) {
					empform.setKeyword(EncodingUtil.encodeURI(empform.getKeyword())) ;
					urlParam.append("&empform.keyword=" + empform.getKeyword()) ;
				}
				
				//将参数保存到request范围，便于分页
				this.getBaseform().getRequest().setAttribute("urlParam",urlParam.toString()) ;
				this.getBaseform().getRequest().setAttribute("empform",empform) ;
			}
			
			this.pageView.setPageModel(this.empMgr.managerEmpManager(empform,this.pageView.getFirstResult(),this.pageView.getMaxResult())) ;
			this.getBaseform().getRequest().setAttribute("pageView",pageView) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		return "managerEmployee" ; 
	}
	
	/** 按ID查询员工 */
	public String getByIdEmployee() {
		
		this.getBaseform().getRequest().setAttribute("empInfo", this.empMgr.loadByEmpNoManager(emp.getEmpNo())) ;
		
		if("showEmp".equals(this.getBaseform().getType())) {
			return "showByIdEmp" ;
		} else if("modifyEmp".equals(this.getBaseform().getType())) {
			return "modifyEmp" ;
		}
		return "showByIdEmp" ;
	}
	
	/** 按ID修改员工 */
	public String modifyEmployee() throws Exception {
		this.empMgr.updateEmpManager(emp) ;
		
		return "modifySuc" ;
	}
	
	

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public EmployeeForm getEmpform() {
		return empform;
	}

	public void setEmpform(EmployeeForm empform) {
		this.empform = empform;
	}

	public PageView<Employee> getPageView() {
		return pageView;
	}

	public void setPageView(PageView<Employee> pageView) {
		this.pageView = pageView;
	}

	public void setEmpMgr(EmployeeManager empMgr) {
		this.empMgr = empMgr;
	}

	public void setOrgMgr(OrganizationManager orgMgr) {
		this.orgMgr = orgMgr;
	}

	public void setDeptMgr(DepartmentManager deptMgr) {
		this.deptMgr = deptMgr;
	}

	public UploadFileForm getFileform() {
		return fileform;
	}

	public void setFileform(UploadFileForm fileform) {
		this.fileform = fileform;
	}
}
